#include <fstream>
#include <iostream>

#include "EEExpMain.h"

#include <maya/MArgList.h>
#include <maya/MFileObject.h>
#include <maya/MItDag.h>
#include <maya/MDagPath.h>
#include <maya/MGlobal.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MItDependencyNodes.h>
#include <maya/MObject.h>
#include <maya/MObjectArray.h>
#include <maya/MStatus.h>
#include <maya/MString.h>
#include <maya/MStringArray.h>
#include <maya/MFnMesh.h>
#include <maya/MPxFileTranslator.h>

EEWorldExporter::EEWorldExporter(void)
{
	m_bShort = false;
}

EEWorldExporter::~EEWorldExporter(void)
{
}

MStatus	EEWorldExporter::reader( const MFileObject& file,
				   const MString& options,
				   FileAccessMode mode)
{
	std::cerr << "[ERROR] This is an exporter plugin only\n" << std::endl;
	return MS::kFailure;
}

MStatus	EEWorldExporter::writer( const MFileObject& file,
				   const MString& options,
				   FileAccessMode mode )
{
	MStatus     	stat;
	const MString	namesonlyFlag	("namesonly");
	m_bShort		= true;

	//	process the option string passed in from the option script
	if (options.length() > 0) 
	{
        int i, length;
        // Start parsing.
        MStringArray optionList;
        MStringArray theOption;

        options.split(';', optionList);

		//	process each option in the string
		//
		length = optionList.length();
		for( i = 0; i < length; ++i )
		{
            theOption.clear();

			//	split the option using the '=' character. This should make 
			//	theOption[0] hold the option name, and theOption[1] hold the
			//	value of the current option
			//
            optionList[i].split( '=', theOption );

            if( theOption[0] == namesonlyFlag && theOption.length() > 1 ) 
			{
                if(theOption[1].asInt()>0) 
					m_bShort = true;
				else 
					m_bShort = false;
            }
        }
	}
	
	if( mode == MPxFileTranslator::kExportActiveAccessMode )
	{
		std::cerr << "As yet this does not support export selected\nExporting all instead\n";
	}

	FILE* pFile = NULL;

	const char* filename = file.fullName().asChar();
	if(fopen_s(&pFile, filename, WRITE_ASCII ? "w+" : "wb+") == -1)
		std::cerr << "[ERROR] Could not open file";

	if(!pFile) 
	{
		std::cerr << "[ERROR] Could not open file for writing " << file.fullName().asChar() << std::endl;
		return MS::kFailure;
	}
	else 
	{
		if(writeData(pFile) == MS::kFailure)
			std::cerr << "[ERROR] Could not write file " << file.fullName().asChar() << std::endl;

		if(fclose(pFile) == -1)
			std::cerr << "[ERROR] Could not close file";
		pFile = NULL;
	}

	return MS::kSuccess;
}

MString EEWorldExporter::defaultExtension () const {
	return g_worldFileFormat;
}

MPxFileTranslator::MFileKind EEWorldExporter::identifyFile(	const MFileObject& fileName,
						 const char* buffer,
						 short size) const {
	const char* str = fileName.name().asChar();
	unsigned int len = fileName.name().length();

	return (str[len-3] == g_worldFileFormat[0] && 
			str[len-2] == g_worldFileFormat[1] && 
			str[len-1] == g_worldFileFormat[2]) ? 
	kCouldBeMyFileType :
	kNotMyFileType;
}